﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Nasa8x.Web.UI
{
    [ToolboxData("<{0}:YouTubePlayer runat=\"server\"></{0}:YouTubePlayer>")]
    public class YouTubePlayer : Control
    {
        private string width = "425";

        private string height = "344";

        private bool allowFullScreen = true;


        public String URL { get; set; }

        public String Width
        {
            get
            {
                return width;
            }
            set
            {
                width = value;
            }
        }

        public String Height
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
            }
        }

        public Boolean AllowFullScreen
        {
            get
            {
                return allowFullScreen;
            }
            set
            {
                allowFullScreen = value;
            }
        }

       public override void RenderControl(HtmlTextWriter writer)

        {
            StringBuilder oBuilder = null;
            if (!string.IsNullOrEmpty(URL))
            {
                oBuilder = new StringBuilder();
                oBuilder.Append("<object width=\"" + Width + "\" height=\"" + Height + "\"><param name=\"movie\" value=\"" + URL + "\">");
                oBuilder.Append("</param><param name=\"allowFullScreen\" value=\"" + AllowFullScreen + "\"></param><param name=\"allowscriptaccess\" value=\"always\"></param>");
                oBuilder.Append("<embed src=\"" + URL + "\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\"");
                oBuilder.Append("allowfullscreen=\"" + AllowFullScreen + "\" width=\"" + Width + "\" height=\"" + Height + "\"></embed></object>");
            }
            var oDiv = new HtmlGenericControl("div");
            oDiv.Attributes.Add("style", "display:inline");
            oDiv.Attributes.Add("id", ClientID);
            oDiv.InnerHtml = oBuilder != null ? oBuilder.ToString() : "Youtube url is empty";
            Controls.Add(oDiv);
            base.RenderControl(writer);

        }
    }
}
